package com.qijian.mold.util;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.context.annotation.Configuration;

import java.net.Inet4Address;
import java.net.UnknownHostException;

/**
 * 雪花 ID 所需 WorkID、DataCenter ID
 * @author anni.zhu
 * @author anni.zhu@king-v.com
 * Created in 2025/3/29 19:36
 */
@Configuration
public class IdWorkerUtils {
    /**
     * workId使用IP生成
     *
     * @return workId
     */
    public static Long getWorkId() {
        try {
            String hostAddress = Inet4Address.getLocalHost ().getHostAddress ();
            int[] ints = StringUtils.toCodePoints (hostAddress);
            int sums = 0;
            for (int b : ints) {
                sums = sums + b;
            }
            return (long) (sums % 32);
        } catch (UnknownHostException e) {
            // 失败就随机
            return RandomUtils.nextLong (0, 31);
        }
    }

    /**
     * dataCenterId使用hostName生成
     *
     * @return dataCenterId
     */
    public static Long getDataCenterId() {
        try {
            String hostName = SystemUtils.getHostName ();
            int[] ints = StringUtils.toCodePoints (hostName);
            int sums = 0;
            for (int i : ints) {
                sums = sums + i;
            }
            return (long) (sums % 32);
        } catch (Exception e) {
            // 失败就随机
            return RandomUtils.nextLong (0, 31);
        }
    }
}
